package com.lxblyw.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.lxblyw.entity.EmployeeProject;
import com.lxblyw.util.DBHelp;
import com.lxblyw.util.RowMapper;

public class EmployeeProjectDao {
	DBHelp<EmployeeProject> db = new DBHelp<EmployeeProject>();

	public void save(EmployeeProject employeeProject) {
		String sql = "INSERT INTO t_employee_project(employeeid,projectid,role) VALUES (?,?,?);";
		db.executeSQL(sql, employeeProject.getEmployeeid(),employeeProject.getProjectid(),employeeProject.getRole());
	}

	public EmployeeProject findEP(String employeeid,String projectid) {
		String sql = "SELECT employeeid,projectid,role FROM t_employee_project WHERE employeeid=? AND projectid=?";
		EmployeeProject ep = db.executeQueryForObject(sql, new EmployeeProjectRowMapper(),employeeid,projectid);
		ep.setEmployee();
		ep.setProject();
		
		return ep;
	}
	
	private class EmployeeProjectRowMapper implements RowMapper<EmployeeProject> {

		public EmployeeProject mapperRow(ResultSet rs) throws SQLException {
			EmployeeProject ep = new EmployeeProject();
			ep.setEmployeeid(rs.getString("employeeid"));
			ep.setProjectid(rs.getString("projectid"));
			ep.setRole(rs.getString("role"));
			
			return ep;
		}
		
	} 
}
